package com.smartrodin.springbootrabbitmq.config;

import com.smartrodin.springbootrabbitmq.util.Constant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: arnold_nie
 * @date: 2020/6/12 17:07
 * FileName: DirectRabbitConfig
 * Description: 路由模式队列配置
 */
@Configuration
public class DirectRabbitConfig {

    /**
     * 交换机与队列绑定的RoutingKey
     */
    private static final String ROUTING_KEY_USER_REGISTER = "routingKey.userRegister";
    private static final String ROUTING_KEY_ORDER_PAY_SUCCESS = "routingKey.orderPaySuccess";

    /**
     * 创建推送数据队列交换机
     *
     * @return the exchange
     */
    @Bean(Constant.PUSH_DATA_DIRECT_EXCHANGE)
    public Exchange pushDataDirectExchange() {
        return ExchangeBuilder.directExchange( Constant.PUSH_DATA_DIRECT_EXCHANGE ).durable( true ).build();
    }

    /**
     * 推送用户注册数据队列
     *
     * @return
     */
    @Bean(Constant.QUEUE_PUSH_USER_REGISTER)
    public Queue queuePushUserRegister() {
        return new Queue( Constant.QUEUE_PUSH_USER_REGISTER );
    }

    /**
     * 推送订单支付成功
     *
     * @return
     */
    @Bean(Constant.QUEUE_PUSH_ORDER_PAY_SUCCESS)
    public Queue queuePushOrderPaySuccess() {
        return new Queue( Constant.QUEUE_PUSH_ORDER_PAY_SUCCESS );
    }

    /**
     * 声明用户注册数据队列和交换机绑定关系，并且指定RoutingKey
     */
    @Bean
    public Binding userRegisterBindingDirect(@Qualifier(Constant.QUEUE_PUSH_USER_REGISTER) Queue queue,
                                             @Qualifier(Constant.PUSH_DATA_DIRECT_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind( queue ).to( exchange ).with( ROUTING_KEY_USER_REGISTER ).noargs();
    }

    /**
     * 声明订单支付成功，并且指定 RoutingKey
     */
    @Bean
    public Binding orderPaySuccessBindingDirect(@Qualifier(Constant.QUEUE_PUSH_ORDER_PAY_SUCCESS) Queue queue,
                                                @Qualifier(Constant.PUSH_DATA_DIRECT_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind( queue ).to( exchange ).with( ROUTING_KEY_ORDER_PAY_SUCCESS ).noargs();
    }

}
